package network.editor;

import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import xujun.control.XContorlUtil;

/**
 * 读取network.xml文件形成编辑面板需要的资源信息
 * 
 * @author zhangzuoqiang <br/>
 *         Date: Aug 25, 2011
 */
public class EditorResource {
	
	private static final Logger log = Logger.getLogger(EditorResource.class);
	
	private static CellResource imageResource;
	private static GeoResource edgeResource;
	private static GeoResource geoResource;
	private static String defaultFontName;

	private EditorResource() {
	}

	static {
		Document doc = getDocument("network.xml");
		Element root = doc.getDocumentElement();
		NodeList resourceNodes = root.getChildNodes();
		for (int i = 0; i < resourceNodes.getLength(); i++) {
			Node resourceNode = resourceNodes.item(i);
			// 默认字体
			if (resourceNode.getNodeType() == Node.ELEMENT_NODE
					&& resourceNode.getNodeName().equalsIgnoreCase("font")) {
				defaultFontName = XContorlUtil.getStringAttribute(resourceNode,
						"name");
			}
			// 解析图片节点
			if (resourceNode.getNodeType() == Node.ELEMENT_NODE
					&& resourceNode.getNodeName().equalsIgnoreCase("images")) {
				NodeList imageNodes = resourceNode.getChildNodes();
				imageResource = new CellResource();
				createImageVertexResource(imageNodes, imageResource, "image");
			}
			// 解析边节点
			if (resourceNode.getNodeType() == Node.ELEMENT_NODE
					&& resourceNode.getNodeName().equalsIgnoreCase("edges")) {
				NodeList edgeNodes = resourceNode.getChildNodes();
				edgeResource = new GeoResource();
				createGeoResource(edgeNodes, edgeResource, "edge");
			}
			// 解析几何节点
			if (resourceNode.getNodeType() == Node.ELEMENT_NODE
					&& resourceNode.getNodeName()
							.equalsIgnoreCase("geometries")) {
				NodeList edgeNodes = resourceNode.getChildNodes();
				geoResource = new GeoResource();
				createGeoResource(edgeNodes, geoResource, "geometry");
			}
		}
	}

	private static void createImageVertexResource(NodeList imageNodeList,
			CellResource resource, String nodeName) {
		for (int i = 0; i < imageNodeList.getLength(); i++) {
			Node imageNode = imageNodeList.item(i);
			if (imageNode.getNodeType() == Node.ELEMENT_NODE
					&& imageNode.getNodeName().equalsIgnoreCase(nodeName)) {
				String text = XContorlUtil
						.getStringAttribute(imageNode, "name");
				Icon icon = XContorlUtil.getIconAttribute(imageNode, "icon");
				String iconPath = XContorlUtil.getStringAttribute(imageNode,
						"icon");
				resource.getNames().add(text);
				resource.getIcons().add(icon);
				resource.getIconPaths().add(iconPath);
			}
		}
	}

	private static void createGeoResource(NodeList imageNodeList,
			GeoResource resource, String nodeName) {
		for (int i = 0; i < imageNodeList.getLength(); i++) {
			Node imageNode = imageNodeList.item(i);
			if (imageNode.getNodeType() == Node.ELEMENT_NODE
					&& imageNode.getNodeName().equalsIgnoreCase(nodeName)) {
				String text = XContorlUtil
						.getStringAttribute(imageNode, "name");
				Icon icon = XContorlUtil.getIconAttribute(imageNode, "icon");
				String style = XContorlUtil.getStringAttribute(imageNode,
						"style");
				resource.getNames().add(text);
				resource.getIcons().add(icon);
				resource.getStyles().add(style);
			}
		}
	}

	public static CellResource getImageResource() {
		return imageResource;
	}

	public static GeoResource getEdgeResource() {
		return edgeResource;
	}

	public static GeoResource getGeoResource() {
		return geoResource;
	}

	public static Document getDocument(String filePath) {
		DocumentBuilderFactory docFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder docBuilder;
		Document doc = null;
		try {
			docBuilder = docFactory.newDocumentBuilder();
			doc = docBuilder.parse(XContorlUtil.getXMLFile(filePath));
		} catch (Exception e) {
			log.error(filePath + "加载或解析出错:" + e.getMessage());
		}
		return doc;
	}

	public static String getDefaultFontName() {
		return defaultFontName;
	}
}

class CellResource {
	List<String> names = new ArrayList<String>();
	List<Icon> icons = new ArrayList<Icon>();
	List<String> iconPaths = new ArrayList<String>();

	public List<String> getNames() {
		return names;
	}

	public void setNames(List<String> names) {
		this.names = names;
	}

	public List<Icon> getIcons() {
		return icons;
	}

	public void setIcons(List<Icon> icons) {
		this.icons = icons;
	}

	public List<String> getIconPaths() {
		return iconPaths;
	}

	public void setIconPaths(List<String> iconPaths) {
		this.iconPaths = iconPaths;
	}
}

class GeoResource {
	List<String> names = new ArrayList<String>();
	List<Icon> icons = new ArrayList<Icon>();
	List<String> styles = new ArrayList<String>();

	public List<String> getNames() {
		return names;
	}

	public void setNames(List<String> names) {
		this.names = names;
	}

	public List<Icon> getIcons() {
		return icons;
	}

	public void setIcons(List<Icon> icons) {
		this.icons = icons;
	}

	public List<String> getStyles() {
		return styles;
	}

	public void setStyles(List<String> styles) {
		this.styles = styles;
	}
}